<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CI_Controller {

	function __construct(){
		parent::__construct();
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
		}
		$this->load->model('admin/member_model','member_model');
		$this->lang->load('main', 'thai');
	}
	
	public function index()
	{
		$this->member_list();
	}
	
	public function member_list()
	{
		$data['level'] = -1;
		$data['keyword'] = '';
		$data['page'] = 1;
		
		if(isset($_GET)){
			foreach($_GET as $k=>$v){
				$data[$k] = $v;
			}
		}
		
		$data['level1_selected'] = '';
		$data['level2_selected'] = '';
		$data['level3_selected'] = '';
		switch($data['level']){
			case -1 :
				$data['level1_selected'] = 'selected="selected"';
				break;
			case 0 :
				$data['level2_selected'] = 'selected="selected"';
				break;
			case 1 :
				$data['level3_selected'] = 'selected="selected"';
				break;
		}
		
		$lists = $this->member_model->lists($data['page'],50,$data['level'],$data['keyword']);
		$data['lists'] = array();
		$i = 0;
		foreach($lists as $v){
			$data['lists'][$i]['id'] = $v->id;
			$data['lists'][$i]['username'] = $v->username;
			$data['lists'][$i]['name'] = $v->firstname.' '.$v->lastname;
			$data['lists'][$i]['email'] = $v->email;
			$data['lists'][$i]['c_id'] = $v->c_id;
			$data['lists'][$i]['c_name'] = $v->name;
			$data['lists'][$i]['c_email'] = $v->c_email;
			$data['lists'][$i]['status'] = $v->confirm?'confirmed':'unconfirmed';
			$data['lists'][$i]['approve'] = $v->active?'approved':'not approve';
			
			$data['lists'][$i]['w_approve'] = '';
			$this->load->model('user/user_model','user_model');
			$website_info = $this->user_model->getCompanyWebsiteByCompanyId($v->c_id);
			if($website_info){
				
				$w_approve = 'not approve';
				if($website_info['active']==1){
					$w_approve = 'approved';
				}
				
				$data['lists'][$i]['w_approve'] = '<a href = "'.base_url().'admin/member/web_approve/'.$website_info['id'].'" class = "btn">'.$w_approve.'</a>';
			}
			
			if($v->gold_active==1){
				if($v->gold_start <= time() AND $v->gold_finish >= time()){
					$data['lists'][$i]['level'] = $this->lang->line('gold_level').
				'<br /><span class="gold">start '.date('d/m/Y',$v->gold_start).' / expire '.date('d/m/Y',$v->gold_finish).'</span>';
				}else{
					$data['lists'][$i]['level'] = $this->lang->line('general_level');
				}
			}else{
				$data['lists'][$i]['level'] = $this->lang->line('general_level');
			}
			
			$i++;
		}
		
		$submenu['submenu_1'] = 'current';
		$submenu['submenu_2'] = '';
		
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view');
		$this->template->write_view('submenu','member_submenu_view',$submenu);
		$this->template->write_view('content','member_view',$data);
		$this->template->render();
	}
	
	//public function add()
	//{
	//	if($this->session->userdata('admin_logged_in') == FALSE){
	//		redirect(base_url()."admin");
	//		exit();
	//	}
	//	
	//	$menu['menu_1'] = '';
	//	$menu['menu_2'] = '';
	//	$menu['menu_3'] = '';
	//	$menu['menu_4'] = 'current';
	//	$menu['menu_5'] = '';
	//	
	//	$data['member_location'] = $this->member_model->get_member_location();
	//	
	//	$data['message'] = $this->session->flashdata('message');
	//	$this->template->set_template('admin');
	//	$this->template->write_view('menu','menu_view',$menu);
	//	$this->template->write_view('content','member_add_view',$data);
	//	$this->template->render();
	//}
	//
	//public function add_process()
	//{
	//	if($this->session->userdata('admin_logged_in') == FALSE){
	//		redirect(base_url()."admin");
	//		exit();
	//	}
	//	
	//	if(isset($_POST['location']) AND $_POST['date_start']!='' AND isset($_POST['date_finish'])){
	//		$input['location'] = $_POST['location'];
	//		$input['date_start'] = strtotime($_POST['date_start']);
	//		$input['date_finish'] = strtotime($_POST['date_finish'].' 23:59:59');
	//		$input['url'] = ($_POST['url']);
	//		$input['code'] = ($_POST['code']);
	//		$input['used'] = $_POST['used'];
	//		$input['date_add'] = time();
	//		$input['remark'] = ($_POST['remark']);
	//		$id = $this->member_model->add($input);
	//		
	//		if($id!=0){
	//			if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
	//				$this->load->library('ciupload');
	//				$this->ciupload->Upload($_FILES['img']);
	//				if ($this->ciupload->uploaded){
	//					//$this->ciupload->image_resize = true;
	//					//$this->ciupload->image_ratio_fill = true;
	//					//$this->ciupload->image_x = 156;
	//					//$this->ciupload->image_y = 100;
	//					$this->ciupload->Process(APPPATH.'../assets/image/member/'.$id);
	//					
	//					if ($this->ciupload->processed){
	//						$input['id'] = $id;
	//						$input['img'] = $this->ciupload->file_dst_name;
	//						$result = $this->member_model->update_img($input);
	//						if($result==0){
	//							@unlink(APPPATH.'../assets/image/member/'.$id.'/'.$this->ciupload->file_dst_name);
	//							$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
	//						}else{
	//							redirect(base_url()."admin/member");
	//						}
	//					}else{
	//						$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
	//						redirect(base_url()."admin/member");
	//					}
	//				}else{
	//					$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
	//					redirect(base_url()."admin/member");
	//				}
	//			}else{
	//				redirect(base_url()."admin/member");
	//			}
	//		}else{
	//			$this->session->set_flashdata('message', 'ไม่สามารถบันทึกข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
	//			redirect(base_url()."admin/member/add");
	//		}
	//	}else{
	//		$this->session->set_flashdata('message', 'กรุณากรอกข้อมูลให้ครบถ้วน');
	//		redirect(base_url()."admin/member/add");
	//	}
	//}
	
	public function edit($id)
	{
		$this->load->model('user/user_model','user_model');
		$data['user_info'] = $this->user_model->getUserInfoByID($id);
		$data['company_info'] = $this->user_model->getCompanyInfo($id);
		
		$gold_select = '';
		$general_select = '';
		$data['user_info']['is_gold'] = 'style="display:none;"';
		if($data['user_info']['role_id'] == 1){
			$gold = FALSE;
			$general_select = 'selected="selected"';
		}else{
			if($data['user_info']['gold_active'] != 1){
				$gold = FALSE;
				$general_select = 'selected="selected"';
			}else{
				if($data['user_info']['gold_start'] <= time() AND $data['user_info']['gold_finish'] >= time()){
					$gold = TRUE;
					$gold_select = 'selected="selected"';
					$data['user_info']['is_gold'] = '';
				}else{
					$gold = FALSE;
					$general_select = 'selected="selected"';
				}
			}
		}
		
		if($data['user_info']['gold_start']==0){
			$data['user_info']['gold_start'] = time();
			$data['user_info']['gold_finish'] = time();
		}
		
		$data['user_info']['level'] =<<<ddd
			<select name="level" id="role_id">
				<option value="1" {$general_select}>{$this->lang->line('general_level')}</option>
				<option value="2" {$gold_select}>{$this->lang->line('gold_level')}</option>
			</select>
ddd;
		
		$this->load->model('admin/category_model','category_model');
		$main_cat_id = $this->category_model->getCatParrentId($data['company_info']['cat_id']);
		$category_option = $this->category_model->lists(0,1,500);
		$data['category_option'] = array();
		$i = 0;
		foreach($category_option as $v){
			$data['category_option'][$i]['id'] = $v->id;
			$data['category_option'][$i]['name_th'] = $v->name_th;
			$data['category_option'][$i]['selected'] = '';
			if($v->id==$main_cat_id){
				$data['category_option'][$i]['selected'] = 'selected="selected"';
			}
			$i++;
		}
		
		$sub_category_option = $this->category_model->lists($main_cat_id,1,500);
		$data['sub_category_option'] = array();
		$i = 0;
		foreach($sub_category_option as $v){
			$data['sub_category_option'][$i]['id'] = $v->id;
			$data['sub_category_option'][$i]['name_th'] = $v->name_th;
			$data['sub_category_option'][$i]['selected'] = '';
			if($v->id==$data['company_info']['cat_id']){
				$data['sub_category_option'][$i]['selected'] = 'selected="selected"';
			}
			$i++;
		}
		
		$this->load->model('admin/province_model','province_model');
		$province_option = $this->province_model->lists();
		$data['province_option'] = array();
		$i = 0;
		foreach($province_option as $v){
			$data['province_option'][$i]['PROVINCE_ID'] = $v->PROVINCE_ID;
			$data['province_option'][$i]['PROVINCE_NAME'] = $v->PROVINCE_NAME;
			$data['province_option'][$i]['selected'] = '';
			if($v->PROVINCE_ID==$data['company_info']['province']){
				$data['province_option'][$i]['selected'] = 'selected="selected"';
			}
			$i++;
		}
		
		$data['message'] = $this->session->flashdata('message');
		$submenu['submenu_1'] = 'current';
		$submenu['submenu_2'] = '';
		
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view');
		$this->template->write_view('submenu','member_submenu_view',$submenu);
		$this->template->write_view('content','member_edit_view',$data);
		$this->template->render();
	}
	
	public function edit_process()
	{
		//exit;
		
		if(!isset($_POST['email']) OR $_POST['email']==''){
			redirect(site_url().'admin/member');
		}else{
			$time = time();
			
			$id = $_POST['user_id'];
			$member['id'] = $id;
			$member['role_id'] = $_POST['level'];
			if($member['role_id']!=1){
				$member['gold_start'] = strtotime($_POST['gold_start'].' 00:00:00');
				$member['gold_finish'] = strtotime($_POST['gold_finish'].' 23:59:59');
				$member['gold_update'] = $time;
				$member['gold_active'] = 1;
				$member['gold_regis'] = 1;
			}else{
				$member['gold_start'] = 0;
				$member['gold_finish'] = 0;
				$member['gold_update'] = 0;
				$member['gold_active'] = 0;
				$member['gold_regis'] = 0;
			}
			$member['username'] = $_POST['username'];
			$member['email'] = $_POST['email'];
			$member['firstname'] = $_POST['firstname'];
			$member['lastname'] = $_POST['lastname'];
			$member['tel'] = $_POST['member_tel'];
			$member['modified'] = $time;
			$member['password'] = $_POST['password'];;
			
			if($this->member_model->update_supplier($member)){
				$this->load->model('admin/province_model','province_model');
				$company_id = $_POST['company_id'];
				$company['id'] = $company_id;
				$company['name'] = $_POST['company_name'];
				$company['main_cat_id'] = $_POST['company_category'];
				$company['cat_id'] = $_POST['company_subcat'];
				$company['address'] = $_POST['company_address'];
				$company['district'] = $_POST['company_district'];
				$company['geo_id'] = $this->province_model->get_geo_id($_POST['company_province']);
				$company['province'] = $_POST['company_province'];
				$company['postcode'] = $_POST['company_postcode'];
				$company['country'] = $_POST['company_country'];
				$company['agent'] = $_POST['company_agent'];
				$company['website'] = $_POST['company_website'];
				$company['email'] = $_POST['company_email'];
				$company['tel'] = $_POST['company_tel'];
				$company['fax'] = $_POST['company_fax'];
				$company['brand'] = $_POST['company_brand'];
				$company['c_modified'] = $time;
				
				if($this->member_model->update_company($company)){
					$this->load->library('ciupload');
					$img_msg = '';
					$logo_msg = '';
					if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
						$this->ciupload->Upload($_FILES['img']);
						if ($this->ciupload->uploaded){
							$this->ciupload->image_resize = true;
							$this->ciupload->image_ratio_fill = true;
							$this->ciupload->image_x = 156;
							$this->ciupload->image_y = 100;
							$this->ciupload->Process(APPPATH.'../assets/image/company/'.$company_id);
							
							if ($this->ciupload->processed){
								$input['user_id'] = $id;
								$input['id'] = $company_id;
								$input['img'] = $this->ciupload->file_dst_name;
								$result = $this->member_model->update_company_img($input);
								if($result==0){
									@unlink(APPPATH.'../assets/image/company/'.$company_id.'/'.$this->ciupload->file_dst_name);
									$img_msg = $this->lang->line('image_not_upload');
								}else{
									@unlink(APPPATH.'../assets/image/company/'.$company_id.'/'.$_POST['old_img']);
								}
							}else{
								$img_msg = $this->lang->line('image_not_upload');
							}
						}else{
							$img_msg = $this->lang->line('image_not_upload');
						}
					}
					
					if(isset($_FILES["logoimg"]) && is_uploaded_file($_FILES["logoimg"]["tmp_name"]) && $_FILES["logoimg"]["tmp_name"]!=''){
						$this->ciupload->Upload($_FILES['logoimg']);
						if ($this->ciupload->uploaded){
							$this->ciupload->image_resize = true;
							$this->ciupload->image_ratio_fill = true;
							$this->ciupload->image_x = 35;
							$this->ciupload->image_y = 35;
							$this->ciupload->Process(APPPATH.'../assets/image/company/'.$company_id);
							
							if ($this->ciupload->processed){
								$input['user_id'] = $id;
								$input['id'] = $company_id;
								$input['logo'] = $this->ciupload->file_dst_name;
								$result = $this->member_model->update_company_logo($input);
								if($result==0){
									@unlink(APPPATH.'../assets/image/company/'.$company_id.'/'.$this->ciupload->file_dst_name);
									$logo_msg = $this->lang->line('image_not_upload');
								}else{
									@unlink(APPPATH.'../assets/image/company/'.$company_id.'/'.$_POST['old_logo']);
								}
							}else{
								$logo_msg = $this->lang->line('image_not_upload');
							}
						}else{
							$logo_msg = $this->lang->line('image_not_upload');
						}
					}
					$data['message'] = $this->lang->line('user_info_save');
				}else{
					$data['message'] = $this->lang->line('company_not_save');
					
				}
			}else{
				$data['message'] = $this->lang->line('data_not_save');
			}
			
			$this->session->set_flashdata('message', $data['message']);
			redirect(getenv("HTTP_REFERER"));
		}
	}
	
	public function status($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(($id+0)>0){
			$this->member_model->status($id+0);
		}
		
		redirect(getenv("HTTP_REFERER"));
	}
	
	public function approve($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(($id+0)>0){
			$this->member_model->approve($id+0);
		}
		
		redirect(getenv("HTTP_REFERER"));
	}
	
	public function web_approve($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		$id += 0;
		if(($id)>0){
			$this->load->model('user/user_model','user_model');
			$website_info = $this->user_model->getCompanyWebsite($id);
			$input['active'] = abs($website_info['active']-1);
			$this->user_model->update_website($input , $id);
			//echo $this->db->last_query();
		}
		
		redirect(getenv("HTTP_REFERER"));
	}
	
	function email_existed()
	{
		$email = $_POST['email'];
		$id = $_POST['id'];
		$result = $this->member_model->getUserByEmail($id,$email);
		
		if(isset($result['email'])){
			echo 'false';
		}else{
			echo 'true';
		}
	}
	
	function user_existed()
	{
		$username = $_POST['username'];
		$id = $_POST['id'];
		$result = $this->member_model->getUserByUsername($id,$username);
		
		if(isset($result['username'])){
			echo 'false';
		}else{
			echo 'true';
		}
	}
	
	public function del($id,$cid)
	{
		if(($id+0)>0){
			if($this->member_model->delete($id+0)){
				$this->load->helper('devlism');
				$this->member_model->delete_member_company($id+0);
				devlism_remove_dir(APPPATH.'../assets/image/company/'.$cid);
			}
			redirect(base_url()."admin/member");
		}else{
			redirect(base_url()."admin/member");
		}
	}
	
	public function message()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = '';
		$menu['menu_4'] = 'current';
		$menu['menu_5'] = '';
		
		$submenu['submenu_1'] = '';
		$submenu['submenu_2'] = 'current';
		$submenu['submenu_3'] = '';
		$submenu['submenu_4'] = '';
		$submenu['submenu_5'] = '';
		
		$data['detail'] = htmlspecialchars_decode($this->member_model->get_member_message());
		
		$data['message'] = $this->session->flashdata('message');
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('submenu','member_submenu_view',$submenu);
		$this->template->write_view('content','member_message_view',$data);
		$this->template->render();
	}
	
	public function message_edit_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		if(isset($_POST['detail'])){
			if($this->member_model->update_member_message(htmlspecialchars($_POST['detail']))){
				$this->session->set_flashdata('message', 'บันทึกข้อมูลเรียบร้อยแล้ว');
			}else{
				$this->session->set_flashdata('message', 'ไม่สามารถแก้ไขข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
			}
		}
		
		redirect(base_url()."admin/member/message");
	}
}

/* End of file member.php */
/* Location: ./modules/admin/controllers/member.php */